home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games: Greatest Hits 1996 / Amiga Games: Greatest Hits 1996.iso / archive / userbox / publicdomain / superplay-lib_usr.lha / superplay-lib_USR / SuperPlay / SuperPlay.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  16KB  |  570 lines

  1.  
  2.  /* SuperPlay V4.5
  3.     Multi-Sound-Replayer
  4.     (c) 1993-95 by Andreas R. Kleinert. All rights reserved.
  5.     Compiler     : SAS/C V6.55
  6.     Last changes : 21.10.1995
  7.  */
  8.  
  9. #include "SuperPlay.h"
  10. #include <intuition/intuitionbase.h>
  11.  
  12.  
  13.    /* Help-, Info- and Error-Texts */
  14.  
  15. char entry1_text  [] = "\2331;32;40m"SP_STRDEF_VERSION " " SP_STRDEF_DATE"\2330;32;40m\2333;32;40m (SHAREWARE)\2330;32;40m\n(c) 1994-95 by Andreas Ralph Kleinert. All rights reserved.\n"
  16.                        "Andreas R. Kleinert, Grube Hohe Grethe 23, D-57074 Siegen, Germany.\n";
  17. char entry2_text  [] = "Player program for use with SuperPlay-Library.\n";
  18. char entry3_text  [] = "USAGE : \2330;33;40mSuperPlay\2330;31;40m\n";
  19.  
  20. char ver_text [] = SP_STRDEF_VER SP_STRDEF_VERSION " " SP_STRDEF_DATE;
  21.  
  22. char intuitionlib_text    [] = "You need \42intuition.library\42 V37+ !";
  23. char graphicslib_text    [] = "You need \42graphics.library\42 V37+ !";
  24. char gadtoolslib_text    [] = "You need \42gadtools.library\42 V37+ !";
  25. char asllib_text    [] = "You need \42asl.library\42 V37+ !";
  26. char splib_text      [] = "You need \42superplay.library\42 V4+ !";
  27.  
  28. char no_win_text        [] = "Can't open GUI Window !";
  29. char no_scr_text        [] = "Can't find Workbench Screen !";
  30. char no_memory_text     [] = "Not enough memory !";
  31.  
  32.  
  33. /* *************************************************** */
  34. /* *                             * */
  35. /* * Function Declarations                 * */
  36. /* *                             * */
  37. /* *************************************************** */
  38.  
  39. void __stdargs SPS_HandleGadgets(void);
  40. void __stdargs Leave(char *errtext, long code);
  41.  
  42. ULONG __stdargs SPS_Play(char *filename, ULONG flag);
  43. ULONG __stdargs SPS_PlayRequest(ULONG flag, ULONG *gotname);
  44.  
  45. ULONG __stdargs SPS_Conv(char *filename, ULONG code);
  46. ULONG __stdargs SPS_ConvRequest(ULONG code);
  47.  
  48. ULONG __stdargs SPS_FileRequest(struct SP_RememberPath *sprp, char *title, char *left, char *right);
  49. void  __stdargs SPS_DisplayError(char *text);
  50.  
  51. void __stdargs SP_Printf(char *formatstring, ...);
  52. void __stdargs SP_SPrintf(char *buffer, char *formatstring, ...);
  53.  
  54.  
  55. /* *************************************************** */
  56. /* *                             * */
  57. /* * Additional Base Declarations             * */
  58. /* *                             * */
  59. /* *************************************************** */
  60.  
  61. struct IntuitionBase *IntuitionBase  = N;
  62. struct GfxBase         *GfxBase         = N;
  63. struct Library         *GadToolsBase   = N;
  64. struct Library         *AslBase        = N;
  65. struct SuperPlayBase *SuperPlayBase  = N;
  66.  
  67.  
  68. /* *************************************************** */
  69. /* *                             * */
  70. /* * Globally Accessed Variables             * */
  71. /* *                             * */
  72. /* *************************************************** */
  73.  
  74. struct SPS_SPOList *SPOList = N;
  75. APTR playhandle             = N;
  76.  
  77.  
  78. /* *************************************************** */
  79. /* *                             * */
  80. /* * Compiler Stuff for BackgroundIO             * */
  81. /* *                             * */
  82. /* *************************************************** */
  83.  
  84. long  __stack         = 32768;
  85. char *__procname     = "SuperPlay";
  86. long  __priority     = 1;
  87. long  __BackGroundIO = 1;        /* TRUE : We DO BackGroundIO !     */
  88.  
  89. extern BPTR _Backstdout;        /* NULL, if started from Workbench */
  90.  
  91.  
  92. /* *************************************************** */
  93. /* *                             * */
  94. /* * MAIN                         * */
  95. /* *                             * */
  96. /* *************************************************** */
  97.  
  98. void main(long argc, char **argv)
  99. {
  100.  APTR task;
  101.  
  102.  Forbid();
  103.  task = FindTask(N);
  104.  Permit();
  105.  
  106.  if(task) SetTaskPri(task, __priority);
  107.  
  108.  if(argc && argv[1][0] =='?')
  109.   {
  110.    SP_Printf("%s%s%s", entry1_text, entry2_text, entry3_text);
  111.  
  112.    Leave(N, 0);
  113.   }
  114.  
  115.  if(OS_VER < 37) Leave("You need OS V2.04+ !", 20);
  116.  
  117.  IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37);
  118.  if(!IntuitionBase) Leave(intuitionlib_text, 20);
  119.  
  120.  GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 37);
  121.  if(!GfxBase) Leave(graphicslib_text, 20);
  122.  
  123.  GadToolsBase = (struct Library *) OpenLibrary("gadtools.library", 37);
  124.  if(!GadToolsBase) Leave(gadtoolslib_text, 20);
  125.  
  126.  AslBase = (struct Library *) OpenLibrary("asl.library", 37);
  127.  if(!AslBase) Leave(asllib_text, 20);
  128.  
  129.  SuperPlayBase = (struct SuperPlayBase *) OpenLibrary("superplay.library", 4);
  130.  if(!SuperPlayBase) Leave(splib_text, 20);
  131.  
  132.  SPOList = SPS_GetSPOList();
  133.  if(!SPOList) Leave(no_memory_text, 20);
  134.  
  135.  SPS_HandleGadgets();
  136.  
  137.  Leave(N, 0);
  138. }
  139.  
  140. /* *************************************************** */
  141. /* *                             * */
  142. /* * Leave : Global Exit Function Replacement         * */
  143. /* *                             * */
  144. /* *************************************************** */
  145.  
  146. void __stdargs Leave(char *errtext, long code)
  147. {
  148.  if(Scr)
  149.   {
  150.    if(SP_Main_Wnd) CloseSP_Main_Window();
  151.  
  152.    CloseDownScreen();
  153.   }
  154.  
  155.  if(SPOList)    SPS_FreeSPOList(SPOList);
  156.  if(playhandle) SPL_FreeHandle(playhandle);
  157.  
  158.  if(SuperPlayBase)  CloseLibrary((APTR) SuperPlayBase);
  159.  if(AslBase)        CloseLibrary((APTR) AslBase);
  160.  if(GadToolsBase)   CloseLibrary((APTR) GadToolsBase);
  161.  if(GfxBase)        CloseLibrary((APTR) GfxBase);
  162.  if(IntuitionBase)  CloseLibrary((APTR) IntuitionBase);
  163.  
  164.  if(_Backstdout)
  165.   {
  166.    if(errtext) Write(_Backstdout, errtext, strlen(errtext));
  167.    Close(_Backstdout);
  168.   }
  169.  
  170.  exit(code);
  171. }
  172.  
  173. /* *************************************************** */
  174. /* *                             * */
  175. /* * SPS_HandleGadgets : ...                         * */
  176. /* *                             * */
  177. /* *************************************************** */
  178.  
  179. void __stdargs SPS_HandleGadgets(void)
  180. {
  181.  struct IntuiMessage *msg;
  182.  ULONG class, code, gadid, last_spogad = 0;
  183.  struct Gadget *address;
  184.  struct SPS_SPOEntry   *spe_node = N;
  185.  struct SPO_ObjectNode *spo_node = N;
  186.  ULONG retval = SPERR_NO_ERROR;
  187.  
  188.  struct TagItem __aligned tags[3];
  189.  
  190.  
  191.  spe_node = SPS_GetSPOEntry(SPOList, 0);
  192.  spo_node = spe_node->spe_SPO_ObjectNode;
  193.  
  194.  SetupScreen();
  195.  if(Scr)
  196.   {
  197.    OpenSP_Main_Window();
  198. /* SP_Main_Render(); */
  199.  
  200.    if(SP_Main_Wnd)
  201.     {
  202.      ModifyIDCMP(SP_Main_Wnd,   (BUTTONIDCMP|MXIDCMP|IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW)
  203.                               ^ (IDCMP_INTUITICKS | IDCMP_MOUSEBUTTONS) 
  204.                               |  IDCMP_MOUSEMOVE  | IDCMP_VANILLAKEY | IDCMP_RAWKEY);
  205.  
  206.      tags[0].ti_Tag  = (Tag)   GTLV_Labels;
  207.      tags[0].ti_Data = (ULONG) SPOList;
  208.  
  209.      tags[1].ti_Tag  = (Tag)  TAG_DONE;
  210.      tags[1].ti_Data = (ULONG) N;
  211.  
  212.      GT_SetGadgetAttrsA(SP_Main_Gadgets[GD_SPOGad], SP_Main_Wnd, N, &tags[0]);
  213.  
  214.      for(;;)
  215.       {
  216.        if(retval)
  217.         {
  218.          if(retval == SPERR_ACTION_NOT_SUPPORTED) DisplayBeep(Scr);
  219.           else SPS_DisplayError(SPL_GetErrorString(retval));
  220.  
  221.          retval = SPERR_NO_ERROR;
  222.         }
  223.  
  224.        WaitPort(SP_Main_Wnd->UserPort);
  225.  
  226.        msg = GT_GetIMsg(SP_Main_Wnd->UserPort);
  227.        if(!msg) continue;
  228.  
  229.        class   = msg->Class;
  230.        code    = msg->Code;
  231.        address = msg->IAddress;
  232.        gadid   = address->GadgetID;
  233.  
  234.        GT_ReplyIMsg(msg);
  235.  
  236.        if(class == IDCMP_CLOSEWINDOW) break;
  237.        if(class == IDCMP_VANILLAKEY)
  238.         {
  239.          switch(toupper(code))
  240.           {
  241.            case 'O' : {
  242.                        if(last_spogad < SPOList->spl_NumEntries-1) last_spogad++;
  243.                         else                                       last_spogad = 0;
  244.  
  245.                        tags[0].ti_Tag  = (Tag)   GTLV_Top;
  246.                        tags[0].ti_Data = (ULONG) last_spogad;
  247.  
  248.                        tags[1].ti_Tag  = (Tag)  GTLV_Selected;
  249.                        tags[1].ti_Data = (ULONG) last_spogad;
  250.  
  251.                        tags[2].ti_Tag  = (Tag)  TAG_DONE;
  252.                        tags[2].ti_Data = (ULONG) N;
  253.  
  254.                        GT_SetGadgetAttrsA(SP_Main_Gadgets[GD_SPOGad], SP_Main_Wnd, N, &tags[0]);
  255.  
  256.                        break;
  257.                       }
  258.            case 'C' : { class = IDCMP_GADGETUP; gadid = GD_ConvertGad; break; }
  259.            case 'P' : { class = IDCMP_GADGETUP; gadid = GD_PlayGad;    break; }
  260.            case 'S' : { class = IDCMP_GADGETUP; gadid = GD_StopGad;    break; }
  261.            case 'T' : { class = IDCMP_GADGETUP; gadid = GD_ContGad;    break; }
  262.            case 'F' : { class = IDCMP_GADGETUP; gadid = GD_ForGad;     break; }
  263.            case 'B' : { class = IDCMP_GADGETUP; gadid = GD_BackGad;    break; }
  264.            case 'I' : { class = IDCMP_GADGETUP; gadid = GD_InfoGad;    break; }
  265.            case 'L' : { class = IDCMP_GADGETUP; gadid = GD_LEDGad;     break; }
  266.            default : break;
  267.           }
  268.         }
  269.        if(   class == IDCMP_GADGETUP
  270.           || class == IDCMP_GADGETDOWN)
  271.         {
  272.          switch(gadid)
  273.           {
  274.            case GD_SPOGad :
  275.             {
  276.              spe_node = SPS_GetSPOEntry(SPOList, code);
  277.              spo_node = spe_node->spe_SPO_ObjectNode;
  278.  
  279.              last_spogad = code;
  280.  
  281.              break;
  282.             }
  283.            case GD_ConvertGad :
  284.             {
  285.              retval = SPS_ConvRequest(spe_node->spe_ObjectSubCode);
  286.  
  287.              break;
  288.             }
  289.            case GD_PlayGad :
  290.             {
  291.              ULONG gotname;
  292.  
  293.              retval = SPS_PlayRequest(TRUE, &gotname);
  294.  
  295.              break;
  296.             }
  297.            case GD_StopGad :
  298.             {
  299.              if(playhandle) SPL_StopReplay(playhandle);
  300.               else SPS_DisplayError("No File loaded");
  301.  
  302.              break;
  303.             }
  304.            case GD_ContGad :
  305.             {
  306.              if(playhandle) retval = SPL_ContinueReplay(playhandle);
  307.               else SPS_DisplayError("No File loaded");
  308.  
  309.              break;
  310.             }
  311.            case GD_ForGad :
  312.             {
  313.              if(playhandle) retval = SPL_FastForward(playhandle);
  314.               else SPS_DisplayError("No File loaded");
  315.  
  316.              break;
  317.             }
  318.            case GD_BackGad :
  319.             {
  320.              if(playhandle) retval = SPL_FastBackward(playhandle);
  321.               else SPS_DisplayError("No File loaded");
  322.  
  323.              break;
  324.             }
  325.            case GD_InfoGad :
  326.             {
  327.              if(playhandle) retval = SPL_FileInfoRequest(playhandle, SP_Main_Wnd, N);
  328.               else SPS_DisplayError("No File loaded");
  329.  
  330.              break;
  331.             }
  332.            case GD_LEDGad :
  333.             {
  334.              UBYTE *address = (UBYTE *) 0xbfe001;
  335.  
  336.              if(*address & 2) /* it's OFF, turn it ON */ *address &= 253;
  337.               else            /* else turn it OFF     */ *address |= 2;
  338.  
  339.              break;
  340.             }
  341.            default : break;
  342.           }
  343.         }
  344.       }
  345.  
  346.      while( msg = GT_GetIMsg(SP_Main_Wnd->UserPort) ) GT_ReplyIMsg(msg);
  347.     }else Leave(no_win_text, 20);
  348.   }else Leave(no_scr_text, 20);
  349. }
  350.  
  351. /* *************************************************** */
  352. /* *                             * */
  353. /* * SPS_Play : Play Routine                         * */
  354. /* *                             * */
  355. /* *************************************************** */
  356.  
  357. ULONG __stdargs SPS_Play(char *filename, ULONG flag)
  358. {
  359.  ULONG retval = SPERR_NO_ERROR;
  360.  
  361.  if(playhandle) SPL_FreeHandle(playhandle);
  362.  
  363.  playhandle = SPL_AllocHandle(N);
  364.  if(playhandle)
  365.   {
  366.    if(!(retval = SPL_InitHandleAsDOS(playhandle, N)))
  367.     {
  368.      if(flag) retval = SPL_SuperPlay(playhandle, filename);
  369.       else    retval = SPL_ReadPlayData(playhandle, filename);
  370.  
  371.     }else retval = SPERR_NO_HANDLE;
  372.   }
  373.  
  374.  return(retval);
  375. }
  376.  
  377. /* *************************************************** */
  378. /* *                             * */
  379. /* * SPS_PlayRequest : FileRequest, then SPS_Play()  * */
  380. /* *                             * */
  381. /* *************************************************** */
  382.  
  383. struct SP_RememberPath __aligned sprp_show;
  384.  
  385. ULONG __stdargs SPS_PlayRequest(ULONG flag, ULONG *gotname)
  386. {
  387.  char *filename = sprp_show.rp_Path;
  388.  ULONG retval = SPERR_NO_ERROR;
  389.  
  390.  if(flag) *gotname = SPS_FileRequest(&sprp_show, "Select file to play:", "Play", "Cancel");
  391.   else    *gotname = SPS_FileRequest(&sprp_show, "Select file to convert:", "Convert", "Cancel");
  392.  
  393.  if(filename[0]==(char) 0) *gotname = FALSE;
  394.  
  395.  if(*gotname) retval = SPS_Play(filename, flag);
  396.  
  397.  return(retval);
  398. }
  399.  
  400. /* *************************************************** */
  401. /* *                             * */
  402. /* * SPS_Conv : Conv Routine                         * */
  403. /* *                             * */
  404. /* *************************************************** */
  405.  
  406. ULONG __stdargs SPS_Conv(char *filename, ULONG code)
  407. {
  408.  ULONG retval = SPERR_NO_ERROR;
  409.  APTR savehandle = N;
  410.  
  411.  savehandle = SPL_AllocHandle(N);
  412.  if(savehandle)
  413.   {
  414.                retval = SPL_SetWriteType(savehandle, code, N);
  415.    if(!retval) retval = SPL_SetWriteName(savehandle, filename, N);
  416.    if(!retval) retval = SPL_SuperWrite(savehandle, playhandle);
  417.  
  418.    SPL_FreeHandle(savehandle);
  419.   }
  420.  
  421.  return(retval);
  422. }
  423.  
  424. /* *************************************************** */
  425. /* *                             * */
  426. /* * SPS_ConvRequest : FileRequest, then SPS_Conv()  * */
  427. /* *                             * */
  428. /* *************************************************** */
  429.  
  430. struct SP_RememberPath __aligned sprp_save;
  431.  
  432. ULONG __stdargs SPS_ConvRequest(ULONG code)
  433. {
  434.  char *filename = sprp_save.rp_Path;
  435.  ULONG retval = SPERR_NO_ERROR;
  436.  ULONG bool;
  437.  
  438.  retval = SPS_PlayRequest(FALSE, &bool);
  439.  if( (!retval) && bool)
  440.   {
  441.    bool = SPS_FileRequest(&sprp_save, "Enter new filename:", "Done", "Abort");
  442.  
  443.    if(filename[0]==(char) 0) bool = FALSE;
  444.  
  445.    if(bool) retval = SPS_Conv(filename, code);
  446.   }
  447.  
  448.  return(retval);
  449. }
  450.  
  451. /* *************************************************** */
  452. /* *                             * */
  453. /* * SPS_FileRequest : FileRequester Routine         * */
  454. /* *                             * */
  455. /* *************************************************** */
  456.  
  457. ULONG __stdargs SPS_FileRequest(struct SP_RememberPath *sprp, char *title, char *left, char *right)
  458. {
  459.  struct TagItem __aligned tags[16];
  460.  struct FileRequester *request = N;
  461.  ULONG bool = FALSE;
  462.  
  463.  tags[0].ti_Tag  = (Tag)   ASLFR_TitleText;
  464.  tags[0].ti_Data = (ULONG) title;
  465.  
  466.  tags[1].ti_Tag  = (Tag)   ASLFR_PositiveText;
  467.  tags[1].ti_Data = (ULONG) left;
  468.  
  469.  tags[2].ti_Tag  = (Tag)   ASLFR_NegativeText;
  470.  tags[2].ti_Data = (ULONG) right;
  471.  
  472.  tags[3].ti_Tag  = (Tag)   ASLFR_InitialFile;
  473.  tags[3].ti_Data = (ULONG) sprp->rp_File;
  474.  
  475.  tags[4].ti_Tag  = (Tag)   ASLFR_InitialDrawer;
  476.  tags[4].ti_Data = (ULONG) sprp->rp_Dir;
  477.  
  478.  tags[5].ti_Tag  = (Tag)   ASLFR_InitialPattern;
  479.  tags[5].ti_Data = (ULONG) "~(#?.info)";
  480.  
  481.  tags[6].ti_Tag  = (Tag)   ASLFR_Flags1;
  482.  tags[6].ti_Data = (ULONG) FRF_DOPATTERNS;
  483.  
  484.  tags[7].ti_Tag  = (Tag)   TAG_DONE;
  485.  tags[7].ti_Data = (ULONG) N;
  486.  
  487.  
  488.  request = AllocAslRequest(ASL_FileRequest, N);
  489.  if(request)
  490.   {
  491.    if(AslRequest(request, &tags[0]))
  492.     {
  493.      ULONG len;
  494.  
  495.      strcpy(sprp->rp_Dir,  request->rf_Dir);
  496.      strcpy(sprp->rp_File, request->rf_File);
  497.  
  498.      strcpy(sprp->rp_Path, request->rf_Dir);
  499.  
  500.      len = strlen(sprp->rp_Path);
  501.      if(len)
  502.       {
  503.        len--;
  504.  
  505.        if(   (sprp->rp_Path[len] != ':')
  506.            &&(sprp->rp_Path[len] != '/')
  507.            &&(sprp->rp_Path[len] != ' ') ) strcat(sprp->rp_Path, "/");
  508.       }
  509.  
  510.      strcat(sprp->rp_Path, request->rf_File);
  511.  
  512.      bool = TRUE;
  513.     }
  514.  
  515.    FreeAslRequest(request);
  516.   }
  517.  
  518.  return(bool);
  519. }
  520.  
  521. void __stdargs SPS_DisplayError(char *text)
  522. {
  523.  ULONG idcmp = N;
  524.  struct EasyStruct *estr;
  525.  
  526.  estr = AllocVec(sizeof(struct EasyStruct), (MEMF_CLEAR|MEMF_PUBLIC));
  527.  if(estr)
  528.   {
  529.    estr->es_StructSize   = sizeof(struct EasyStruct);
  530.    estr->es_Flags        = N;
  531.    estr->es_Title        = "SuperPlay Message";
  532.    estr->es_TextFormat   = text;
  533.    estr->es_GadgetFormat = "Confirm";
  534.  
  535.    EasyRequestArgs(SP_Main_Wnd, estr, &idcmp, N);
  536.  
  537.    FreeVec(estr);
  538.   }
  539. }
  540.  
  541. /* *************************************************** */
  542. /* *                                                 * */
  543. /* * SP_Printf : Replacement for printf()-Output     * */
  544. /* *                                                 * */
  545. /* *************************************************** */
  546.  
  547. #define KIP_BUFLEN (1200)
  548.  
  549. extern void Fmt(void);
  550.  
  551. extern BPTR _Backstdout;
  552.  
  553. void __stdargs SP_Printf(char *formatstring, ...)
  554. {
  555.  UBYTE *buffer;
  556.  
  557.  if(_Backstdout)
  558.   {
  559.    buffer = (BYTE *) AllocMem(KIP_BUFLEN, MEMF_CLEAR);
  560.    if(buffer)
  561.     {
  562.      RawDoFmt((APTR) formatstring, (APTR) (((ULONG *)&formatstring)+1), (APTR) &Fmt, (APTR) buffer);
  563.  
  564.      Write(_Backstdout, buffer, strlen(buffer));
  565.  
  566.      FreeMem(buffer, KIP_BUFLEN);
  567.     }
  568.   }
  569. }
  570.